home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / port11.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1991-03-14  |  14KB  |  622 lines

  1. /*
  2. *    Port - Do all kinds of wild and crazy things to PC I/O ports
  3. *
  4. *    Rev 1.1        03/13/91
  5. *    John De Armond, Rapid Deployment Systems  (jgd@dixie.com)
  6. *    Copyright 1991, John De Armond, Minimal Rights Reserved
  7. *    
  8. *    Compiled with the supplied project file under Borland C++ 2.0 but
  9. *    otherwise uncontaminated with C++ drool.
  10. *    
  11. *    Edited with MKS vi.  To align indents, set tabstop=4, shiftwidth=4
  12. *
  13. *    Surgeon General's warning:  Caution:  Misuse or abuse of this 
  14. *                                program is dangerous to the health of
  15. *                                your hard disk, your memory, your video
  16. *                                monitor and other such goodies.  Govern
  17. *                                your actions accordingly.
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <dos.h>
  22.  
  23. /***** define a few symbolic constants */
  24. #define BYTE 1
  25. #define WORD 2
  26. #define HEX  3
  27. #define DEC    4
  28. #define IN 5
  29. #define OUT 6
  30. /***************************************/
  31.  
  32. unsigned int port=0, byte=0;
  33. unsigned int save_port, save_byte;
  34. char buf[80];
  35. int entry_mode = HEX;
  36. int byte_mode = BYTE;
  37. int io_mode = OUT;
  38. int quiet=0;
  39. int match_byte;
  40.  
  41. extern char * token();
  42. char *token_ptr;
  43. int is_a_tty = 0;
  44. int echo_command = 0;
  45.  
  46. main(argc,argv)
  47. int argc;
  48. char **argv;
  49. {
  50.     unsigned int gak;
  51.     FILE *fp;
  52.  
  53.     /* yes, I do mean "=" below */
  54.     if ( !(is_a_tty = isatty(0)) ) {    /* check stdin for source */
  55.         echo_command=1;                    /* if we're on a pipe, echo each
  56.                                            command */
  57.     }
  58.  
  59.     if (argc >= 2) {    /* if only 2, then argv[1] is port number in hex */
  60.         sscanf(argv[1],"%x",&port);
  61.     }
  62.     if (argc >=3) {        /* if 3 arguments, then argv[2] is the bit pattern*/
  63.         sscanf(argv[2],"%x",&byte);
  64.     }
  65.  
  66.     printf("PORT v1.1 03/13/91 by John De Armond.  (jgd@dixie.com)\n");
  67.  
  68.     help();
  69.  
  70.     for (;;) {
  71.         prompt();
  72.  
  73.         /* this little trick allows us to redirect a file in and
  74.             then take keyboard input.  This allows configuration files
  75.             to be piped in.
  76.         */
  77.         if (fgets(buf,78,stdin) == NULL) {    /* when we hit end of file, */
  78.             freopen("con", "r",stdin);      /* open the console         */
  79.             is_a_tty = 1;
  80.             echo_command=0;
  81.             continue;
  82.         }
  83.  
  84.         if (echo_command) {
  85.             printf("cmd:%s",buf);
  86.         }
  87.         switch (buf[0]) {
  88.  
  89.             case 'r':            /* read port */
  90.                 if (strlen(buf) >2) {    /* we have a wait specification */
  91.                 token(&buf[1]);
  92.                     if (token_ptr != NULL) {    /* if pattern specified */
  93.                         sscanf(token_ptr,"%x",&match_byte);
  94.                         printf(
  95.                         "Waiting for byte %4.4x.  Hit any key to terminate\n");
  96.                         read_port();
  97.                         while (!kbhit()) {
  98.                             save_byte=byte;
  99.                             read_port();
  100.                             if (byte == match_byte) {
  101.                                 printf("Pattern %4.4x found, hit any key.\n",byte);
  102.                                 byte=save_byte;
  103.                                 break;
  104.                             }
  105.                             byte=save_byte;
  106.                         } /* while */
  107.                         getch();    /* clears the port */
  108.                     }  /* if token_ptr */
  109.                 } else {
  110.                     read_port();
  111.                 }
  112.                 break;
  113.  
  114.             case 'w':            /* write port */
  115.                 if (strlen(buf) >2) {    /* we have a wait specification */
  116.                 token(&buf[1]);
  117.                     if (token_ptr != NULL) {    /* if pattern specified */
  118.                         sscanf(token_ptr,"%x",&match_byte);
  119.                         printf(
  120.                         "Waiting for byte %4.4x.  Hit any key to terminate\n",
  121.                           match_byte);
  122.                         write_port();
  123.                         while (!kbhit()) {
  124.                             save_byte=byte;
  125.                             read_port();
  126.                             if (byte == match_byte) {
  127.                                 printf("Pattern %4.4x found, hit any key.\n",byte);
  128.                                 byte=save_byte;
  129.                                 break;
  130.                             }
  131.                             byte=save_byte;
  132.  
  133.                         } /* while */
  134.                         getch();    /* clears the port */
  135.                     }  /* if token_ptr */
  136.                 } else {
  137.                     write_port();
  138.                 }
  139.                 break;
  140.  
  141.             case 'e':            /* enter byte in default mode */
  142.                 switch (buf[1]) {
  143.                 case 'x':        /* enter byte in hex mode*/
  144.                     token(&buf[2]);
  145.                     gak=0;
  146.                     sscanf(token_ptr, "%x",&gak);
  147.                     byte = gak;
  148.                     entry_mode=HEX;
  149.                     break;
  150.                 case 'd':        /* enter byte in decimal mode*/
  151.                     token(&buf[2]);
  152.                     gak=0;
  153.                     sscanf(token_ptr, "%u",&gak);
  154.                     byte = gak;
  155.                     entry_mode=DEC;
  156.                     break;
  157.                 default:        /* use the default mode */
  158.                     token(&buf[2]);
  159.                     gak=0;
  160.                     if (entry_mode == HEX)
  161.                         sscanf(token_ptr, "%x",&gak);
  162.                     else
  163.                         sscanf(token_ptr, "%u",&gak);
  164.                     byte = gak;
  165.                     break;
  166.  
  167.                 }
  168.                 break;
  169.  
  170.             case 't':            /* toggle designated bit */
  171.                 if (buf[1] == 'a') {
  172.                     byte = ~ byte;    /* special case, flip all bits */
  173.                     break;
  174.                 }
  175.  
  176.                 token(&buf[1]);
  177.                 sscanf(token_ptr, "%d", &gak);
  178.                 toggle_bit(gak);
  179.                 break;
  180.  
  181.             case 'i':
  182.                 port++;
  183.                 break;
  184.  
  185.             case 'd':
  186.                 port--;
  187.                 break;
  188.  
  189.             case 'm':            /* set entry mode, hex, dec, byte, word */
  190.                 switch (buf[1]) {
  191.                     case 'x':
  192.                         entry_mode = HEX;
  193.                         break;
  194.                     case 'd':
  195.                         entry_mode = DEC;
  196.                         break;
  197.                     case 'b':
  198.                         byte_mode = BYTE;
  199.                         break;
  200.                     case 'w':
  201.                         byte_mode = WORD;
  202.                         break;
  203.                     case '\0':    /* nothing entered */
  204.                     default:
  205.                         break;
  206.                 }
  207.                 break;
  208.  
  209.             case 'c':            /* clear all bits */
  210.                 byte = 0;
  211.                 break;
  212.             case 's':            /* set all bits */
  213.                 byte = ~0;
  214.                 break;
  215.             case 'p':            /* set port address */
  216.                 token(&buf[1]);
  217.                 if (entry_mode == HEX)
  218.                     sscanf(token_ptr, "%x",&gak);
  219.                 else
  220.                     sscanf(token_ptr, "%u",&gak);
  221.                 if (gak)
  222.                     port = gak;
  223.                 break;
  224.  
  225.             case 'q':            /* quit */
  226.                 puts("");
  227.                 exit(0);
  228.  
  229.             case 'g':            /* go continuously, repeat last I/O op */
  230.                 switch (buf[1]) {
  231.                     case 'i':        /* input mode */
  232.                         rep_io(IN);
  233.                         break;
  234.                     case 'o':
  235.                         rep_io(OUT);
  236.                         break;
  237.                     default:
  238.                         puts("Say What???");
  239.                         break;
  240.                 }
  241.                 break;
  242.  
  243.             case '<':            /* get commands from a file */
  244.                 token(&buf[1]);
  245.                 if ( freopen(token_ptr,"r",stdin) == NULL ) {
  246.                     printf("Cannot open file\n");
  247.                     break;    /* let the check at the head of the loop */
  248.                 }            /* reopen the console */
  249.                 is_a_tty=0;
  250.                 echo_command=1;
  251.                 break;
  252.  
  253.             case '?':            /* show help */
  254.                 help();
  255.                 break;
  256.             default:
  257.                 puts("Say What???");
  258.         } /* switch buf[0] */
  259.         print_it();
  260.  
  261.     }    /* for (;;) */
  262.  
  263. } /* main */
  264.  
  265. print_it()
  266. {
  267.     int i,j;
  268.     unsigned int bit;
  269.     extern char * to_bin();
  270.  
  271.     if (is_a_tty) {
  272.         printf("                                     Bits\n");
  273.         printf("                             5432 1098 7654 3210\n");
  274.     }
  275.     if (entry_mode == HEX) {
  276.         printf("Port:  %4.4x    Byte:  %4.4x,  %s  ", port, byte, to_bin(byte));
  277.         printf("Mode= HEX  ");
  278.     } else {
  279.         printf("Port: %5.5u    Byte: %5.5u,  %s  ", port, byte, to_bin(byte));
  280.         printf("Mode= Decimal  ");
  281.     }
  282.     if (byte_mode == BYTE) {
  283.         printf("Data= BYTE\n");
  284.     } else {
  285.         printf("Data= WORD\n");
  286.     }
  287. }
  288.  
  289. /* convert an unsigned int to a binary string */
  290. char *
  291. to_bin(bit)
  292. unsigned int bit;
  293. {
  294.  
  295.     static char string[25];
  296.     int i,j;
  297.  
  298.     memset(string,0,25);
  299.  
  300.     /* convert the byte to binary */
  301.     for (i=0,j=0; i<16; i++,j++) {
  302.         if ( bit & 0x01) {
  303.             string[18-j] = '1';
  304.         } else {
  305.             string[18-j] = '0';
  306.         }
  307.         if (!((i+1)%4) && i>1)
  308.             string[18- ++j] = ' ';
  309.  
  310.         bit = bit >>1;
  311.     }
  312.  
  313.     return (string);
  314. }
  315.  
  316. prompt()
  317. {
  318.     if (is_a_tty) {
  319.         printf("Command (help-?): ");
  320.     }
  321.     return;
  322. }
  323.  
  324. read_port()
  325. {
  326.  
  327.     if (byte_mode == BYTE) {
  328.         byte = inportb(port);
  329.     } else {
  330.         byte = inport(port);
  331.     }
  332.  
  333.     return;
  334. }
  335.  
  336. write_port()
  337. {
  338.     if (byte_mode == BYTE) {
  339.         outportb(port,byte);
  340.     } else {
  341.         outport(port,byte);
  342.     }
  343.     return;
  344.  
  345. }
  346.  
  347. help()
  348. {
  349.     printf("\nUsage: port <port_address data>  Arguments are in hex\n");
  350.     printf("\nCommands:\n");
  351.     printf("    \n");
  352.     printf("r           read the port\n");
  353.     printf("w           write the port\n");
  354.     printf("e<xd>       Enter a byte (word in designated mode, hex or decimal)\n");
  355.     printf("m<xdbw>     Mode (x=hex, d=dec, b=byte, w=word)\n");
  356.     printf("t n         Toggle bit <n>\n");
  357.     printf("c           Clear all bits\n");
  358.     printf("s           Set all bits\n");
  359.     printf("p           Set port address\n");
  360.     printf("g<io>       Go In or Go Out - perform action continuously\n");
  361.     printf("i           Increment port address one count\n");
  362.     printf("d           Decrement port address one count\n");
  363.     printf("q           Quit\n");
  364.     printf("<           Redirect c